package com.pkche.core.advanced.form;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 
 * <h6>用于解析类结果</h6>
 *
 * User: Qiang <br/>
 * Describe：反射获取类的基本信息 <br/>
 *
 * @date 2015年3月4日 上午8:30:11
 */
public class Scatter {
	protected Class<?> typeClass;
	protected Object typeObject;

	/**
	 * initialize
	 * 
	 * @param classes
	 */
	public Scatter(Class<?> classes) {
		typeClass = classes;
	}

	/**
	 * initialize
	 * 
	 * @param type
	 */
	public Scatter(Object type) {
		typeObject = type;
		typeClass = type.getClass();
	}

	/**
	 * instantiation the current type object,and return type object
	 */
	public Object newInstance() {
		try {
			typeObject = Class.forName(typeClass.getName()).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			return null;
		}
		return typeObject;
	}

	/**
	 * return entity object
	 * 
	 * @return
	 */
	public Object getEntity() {
		return typeObject;
	}

	/**
	 * return the type field names.
	 * 
	 * @return
	 */
	public String[] getFieldNames() {
		Field[] array = typeClass.getDeclaredFields();
		int size = array.length;
		String[] str = new String[size];
		for (int i = 0; i < size; i++) {
			Field f = array[i];
			str[i] = f.getName();
		}
		return str;
	}

	/**
	 * return field type
	 * 
	 * @param fieldName
	 * @return
	 */
	public Field getFiled(String name) {
		Field field = null;
		try {
			field = typeClass.getDeclaredField(name);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			return null;
		}
		return field;
	}

	/**
	 * return the name method
	 * 
	 * @param name
	 * @return
	 */
	public Method getMethod(String name) {
		// 获取类中的所有方法
		Method[] method = typeClass.getMethods();
		for (Method mod : method) {
			if (mod.getName().equals(name)) {
				return mod;
			}
		}
		return null;
	}

	/**
	 * 执行指定名称的get方法获取返回值
	 * 
	 * @param name
	 * @return
	 */
	public Object getter(String name) {
		String method = "get" + name.substring(0, 1).toUpperCase()
				+ name.substring(1);
		try {
			return typeClass.getMethod(method).invoke(typeObject);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 指定指定名称的set方法
	 * 
	 * @param name
	 * @param value
	 */
	public void setter(String name, Object value) {
		String method = "set" + name.substring(0, 1).toUpperCase()
				+ name.substring(1);
		try {
			typeClass.getMethod(method, value.getClass()).invoke(typeObject,
					value);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
